import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hexcolor/hexcolor.dart';
import '../../../entity/record/points_entity.dart';
import '../../../resources/colours.dart';
import '../../../resources/styles.dart';
import '../../../utils/routes.dart';
import '../../../utils/utils.dart';
import '../../../widgets/base/qkd_text.dart';
import '../../../widgets/base/qkd_dialog.dart';

class DataLine extends StatelessWidget{
  const DataLine({super.key, this.team, this.isHeader = false, this.qualify, this.headType, this.index,required this.recordType});
  const DataLine.soccer({super.key, this.team, this.isHeader = false, this.qualify, this.headType, this.index}):recordType = 0;
  const DataLine.basketball({super.key, this.team,this.isHeader = false, this.qualify, this.headType, this.index}):recordType = 1;

  final int recordType;
  final RankingTeam? team;
  final RankingQualify? qualify;
  final bool isHeader;
  final String? headType;
  final int? index;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: () {
        Utils.onEvent("data_match_team_view",params: {"team_id": team?.teamId});
        Get.toNamed(
          recordType == 0?Routes.zqTeamDetail:Routes.lqTeamDetail,
          arguments: team?.teamId
        );
      },
      child: Stack(
        alignment: Alignment.topCenter,
        children: [
          Container(
            color: (index ?? 0) >= ((qualify?.beginRank ?? 0) - 1) &&
              (index ?? 0) <= ((qualify?.endRank ?? -1) - 1)
              ? HexColor(qualify?.color ?? ("#FFFFFF")) : null,
            height: isHeader?null:50,
            padding: EdgeInsets.only(right: isHeader?0:16),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                if(isHeader)...[
                  Expanded(
                    flex: 5,
                    child: QkdText(headType ?? "",
                      fontSize: 13,color: Colours.grey66
                    ).paddingOnly(left: 8),
                  )
                ]else...[
                  Expanded(
                    child: Container(
                      width: 36,
                      alignment: Alignment.center,
                      child: QkdText("${team?.rank ?? "-"}",
                        fontSize: 13,color: Colours.grey66
                      )
                    ),
                  ),
                  Flexible(
                    flex: 1,
                    fit: FlexFit.tight,
                    child: Container(
                      alignment: Alignment.centerLeft,
                      child: ClipRRect(
                        borderRadius: BorderRadius.circular(1),
                        child: CachedNetworkImage(
                            width: 24,
                            height: 24,
                            placeholder: (context, url) =>
                                Styles.placeholderIcon(),
                            errorWidget: (context, url, error) => Image.asset(
                                Utils.getImgPath("${recordType == 0?"zq":"lq"}_placeholder.png")),
                            imageUrl: team?.teamLogo ?? ""),
                      ),
                    ),
                  ),
                  Flexible(
                    flex: 3,
                    fit: FlexFit.tight,
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        QkdText(team?.teamName ?? "",
                          fontSize: 13,
                          maxLines: 2,
                         ),
                      ],
                    ),
                  )
                ],
                Flexible(
                  flex: 8,
                  child: _data()
                ),
              ],
            ),
          ),
          if(team?.explain.hasValue == true)
          Positioned(
            top: 0,
            bottom: 0,
            right: 8,
            child: GestureDetector(
              onTap: () => Get.dialog(QkdDialog.single("扣分说明",
                  content: team?.explain ?? "",
                  confirmText: "我知道了")),
              child: Image.asset(
                  width: 12,
                  height: 12,
                  Utils.getImgPath("question_mark.png")),
            )
          ),
          if(qualify != null && index == (qualify?.beginRank ?? 0)-1)
          Container(
            decoration: BoxDecoration(
              color: qualify?.tagColor != null && qualify?.tagColor != "#ffffff"
                ? HexColor(qualify?.tagColor ?? ("#000000")) : null,
              borderRadius: const BorderRadius.vertical(bottom: Radius.circular(4))),
            padding: const EdgeInsets.symmetric(horizontal: 8),
            child: QkdText(qualify?.name ?? "",
              fontSize: 10, color: Colours.white
            )
          )
        ],
      ),
    );
  }

  Widget _data(){
    List<double> size = recordType == 0?
    [28,60,45,28,28]:[60,45,28,45];
    List<String> header = recordType == 0?
    ["赛","胜/平/负","进/失","净","积分"]:["胜/负","胜率","胜差","近况"];
    List<String>? data = recordType == 0?team?.dataLineSoccer:team?.dataLineBasketball;
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: List.generate(
        size.length,
        (index) => Container(
          width: size[index],
          alignment: Alignment.center,
          child: QkdText(
            isHeader?header[index]:data?[index] ?? "",
            fontSize: 13,
            color: isHeader?Colours.grey66:Colours.text
          )
        )
      )
    );
  }
}
